package com.easemob.helpdesk.widget.pickerview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnItemSelectedListener;
import com.bigkoo.pickerview.view.BasePickerView;
import com.housekeeperapp.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by liyuzhao on 16/3/11.
 */
public class SimplePickerView extends BasePickerView implements View.OnClickListener{

    private Context mContext;
    private View btnSave;
    private View btnClose;
    private TextView title;

    private WheelView wv;
    private Object parent;
    private View objView;
    private ArrayList<String> valueArray;

    public SimplePickerView(Context context, final Object parent, final ArrayList<String> valueArray, final View view, String titleStr){
        super(context);

        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.popup_picker_online, contentContainer);
        btnSave = findViewById(R.id.tv_save);
        btnSave.setOnClickListener(this);

        btnClose = findViewById(R.id.tv_close);
        btnClose.setOnClickListener(this);

        title = (TextView) findViewById(R.id.tv_title);
        title.setText(titleStr);

        wv = (WheelView) findViewById(R.id.wheelview);
        this.parent = parent;
        this.valueArray = valueArray;
        this.objView = view;

//        ArrayList<String> list = new ArrayList<>();
//        list.addAll(list);
        wv.setCyclic(false);
        wv.setTextSize(18);
//        wv.setOnItemSelectedListener(new OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(int index) {
//                if (parent instanceof SimplePickSelectItemListener){
//                    ((SimplePickSelectItemListener)parent).simplePickerSelect(wv.getCurrentItem(), valueArray, view);
//                }
//            }
//        });

        wv.setAdapter(new ArrayWheelAdapter(valueArray));
    }

    public SimplePickerView(Context context, ArrayList<String> list){
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.popup_picker_online, contentContainer);
//        btnSave = findViewById(R.id.tv_save);
//        btnSave.setOnClickListener(this);
        wv = (WheelView) findViewById(R.id.wheelview);
        wv.setCyclic(false);
        wv.setAdapter(new ArrayWheelAdapter(list));
    }

    public SimplePickerView(Context context,ArrayWheelAdapter  adapter){
        super(context);
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.popup_picker_online, contentContainer);
//        btnSave = findViewById(R.id.tv_save);
//        btnSave.setOnClickListener(this);
        wv = (WheelView) findViewById(R.id.wheelview);
        wv.setCyclic(false);
        wv.setAdapter(adapter);
    }

    public int getCurrent(){
        return wv.getCurrentItem();
    }

    @Override
    public void onClick(View view) {

        if(view.getId()==R.id.tv_save){
            if (parent instanceof SimplePickSelectItemListener){
                ((SimplePickSelectItemListener)parent).simplePickerSelect(wv.getCurrentItem(), valueArray, objView);
            }

            dismiss();
        } else if(view.getId()==R.id.tv_close){
            dismiss();
        }

    }


    public interface SimplePickSelectItemListener{
        void simplePickerSelect(int position, ArrayList<String> valueArray, View view);
    }


}
